$('#sc').html(mySetVc()).click(function () { $('#sc').html(mySetVc()) })

$('[name=name]').change(function(){
    isName();
})

$('button').click(function(){

    let name = $('[name="name"]').val();
    let pwd1 = $('[name="pwd1"]').val();
    let pwd2 = $('[name="pwd2"]').val();
    let vc = $('[name="vc"]').val();

    if(pwd1 != pwd2){
        $('[name="pwd1Span"]').html('您输入的两次密码不同');
        $('#sc').html(mySetVc());
        return;
    }else{
        $('[name="pwd1Span"]').html('');
    }

    if(vc.toLowerCase() !== $('#sc').html().toLowerCase()){
        $('[name="vcSpan"]').html('您输入的验证码有误!!!');
        $('#sc').html(mySetVc());
        return;
    }else {
        $('[name="vcSpan"]').html('');
    }
    
    getRegister(name,pwd1);
})

async function isName(){
    let name = $('[name=name]').val();

    const response = await myPromiseJQueryAjax('../server/goods_select_name.php','post',{userName:name},'json');
    console.log(response);

    if(response.code === 0){
        $('[name="nameSpan"]').html('用户名重复,请重新输入!!!').css({color:'red'});
    }else if(response.code === 1){
        $('[name="nameSpan"]').html('用户名可以使用').css({color:'blue'});
    }
}   

async function getRegister(userName,userPwd){
    const response = await myPromiseJQueryAjax('../server/goods_register.php','post',{userName:userName,userPwd:userPwd},'json');
    console.log(response);

    let num = 5;

    $('[name="msg"]').html(`恭喜您注册成功 ${num}秒后跳转 即将跳转登录页`)

    let time = setInterval(() => {
        num--;

        $('[name="msg"]').html(`恭喜您注册成功 ${num}秒后跳转 即将跳转登录页`)
        if(num === 1){
            clearInterval(time);
            window.location.href = `../pages/login.html?id=${response.id}`
        }
    }, 1000);
}